package control;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

import model.Receiver;
import util.SocketUtil;
import view.ChessPanel;
import view.InfoPanel;
import view.LoginPanel;

public class ControlMain extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LoginPanel lp;
	private InfoPanel ipanel;
	private ChessPanel cpanel;

	public ControlMain() {
		setTitle("网络五子棋");
		setVisible(true);
		setLayout(new BorderLayout());
		setSize(1000, 600);
		Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((ds.width - 1000) / 2, (ds.height - 600) / 2);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		lp = new LoginPanel();
		setGlassPane(lp);
		lp.setVisible(true);

		ipanel = new InfoPanel();
		add(ipanel, BorderLayout.EAST);
		cpanel = new ChessPanel();
		add(cpanel);
		Receiver receiver = new Receiver();
		receiver.addObserver(lp);
		receiver.addObserver(ipanel);
		receiver.addObserver(cpanel);
		SocketUtil.getUser().addObserver(cpanel);
		SocketUtil.getUser().addObserver(ipanel);
		SocketUtil.setReceiver(receiver);
		SocketUtil.StartServer();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				try {
					UIManager.setLookAndFeel(new NimbusLookAndFeel());
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				new ControlMain();

			}
		});
	}

}
